/****************************************************************************
 * 2025.8 小庆永不失眠
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using UnityEngine.U2D;

namespace ProjectSurvivor
{
    public partial class ExpUpgradePanel : UIElement, IController
    {
        ResLoader mloader;
        private void Awake()
        {
            mloader = ResLoader.Allocate();
            var iconAtlas = mloader.LoadSync<SpriteAtlas>("icon");


            var expUpgradeSystem = this.GetSystem<ExpUpgradeSystem>();

            foreach (var expUpgradeItem in expUpgradeSystem.Items)
            {
                BtnExpUpgradeItemTemplate.InstantiateWithParent(Upgrade).Self((self) =>
                {
                    var itemChche = expUpgradeItem;
                    self.transform.Find("Icon").GetComponent<Image>().sprite = iconAtlas.GetSprite(itemChche.IconName);
                    self.onClick.AddListener(() =>
                    {
                        Time.timeScale = 1.0f;
                        this.Hide();
                        itemChche.UpGrade();
                        AudioKit.PlaySound("Level_up");
                    });
                    var selfCache = self;
                    selfCache.Hide();
                    itemChche.Visible.RegisterWithInitValue((visible) =>
                    {
                        if(visible)
                        {
                            selfCache.Show();
                            self.GetComponentInChildren<Text>().text = expUpgradeItem.Description;
                            var pairUpgradeName = selfCache.transform.Find("PairedUpgradeName");
                            if (expUpgradeSystem.Pairs.TryGetValue(itemChche.Key, out var pairedName))
                            {
                                var pairedItem = expUpgradeSystem.dic[pairedName];
                               
                                if(pairedItem.CurrentLevel.Value>0 && itemChche.CurrentLevel.Value ==0)
                                {
                                    pairUpgradeName.Show();
                                    pairUpgradeName.transform.Find("Icon").GetComponent<Image>().sprite = iconAtlas.GetSprite(pairedItem.PairedIconName);
                                }
                                else
                                {
                                    pairUpgradeName.Hide();
                                }
                            }
                            else
                            {
                                pairUpgradeName.Hide();
                            }
                        }
                        else
                        {
                            selfCache.Hide();
                        }
                    }).UnRegisterWhenGameObjectDestroyed(selfCache);

                    itemChche.CurrentLevel.RegisterWithInitValue((clevel)=>
                    {
                        selfCache.GetComponentInChildren<Text>().text = itemChche.Description;
                    });
                });
            }
            Debug.Log(Upgrade.childCount);
        }


        protected override void OnBeforeDestroy()
        {
            mloader.Recycle2Cache();
            mloader = null;
        }

        public IArchitecture GetArchitecture()
        {
            return Global.Interface;
        }
    }
}